const app = getApp();
const http = require("../../config/api.js");
const Time = require("../../utils/Timedown.js");
const user = require("../../utils/login.js");
Page({
  data: {
    historyList: [],
    collectionList: [],
    configData: '',
    baseUrlImg: app.globalData.baseUrlImg,
    userInfo: {},
    surplus: false, // 如果小于三天显示剩余天数
    surplusmunber: '',
    mathHours: false, // 是否是小时
    timeshow: false, // 是否是vip
    userInfoShow: false, 
  },
  onLoad: function (options) {
    this.getConfigPage();
  },
  onShow: function(){
    // 获取本地缓存历史记录
    if (!wx.getStorageSync('openid')) {
      let historyData = wx.getStorageSync('historyData') || {};
      if (historyData.dataList) {
        historyData.dataList.slice(0, 5);
        this.setData({
          historyList: historyData
        })
      }
    }else{
      this.getHistoryList();
    }
    let time = Math.round(new Date().getTime());
    let log = {
      //日志类型：unite.通用业务；visit.访问数据；click.点击数据；order.订购数据；error.错误日志
      logType: "visit",
      msg: JSON.stringify({
        mac: app.globalData.userId,
        page: "xcxmy",
        pageName: "梨园行我的",
        time: time,
        appVersionName: "小程序",
        item: 65,
        nodeCode: "001000",
        project: "lyhad",
        userId: app.globalData.memberId,
        deviceId: app.globalData.userId,
        uuid: user.getUuid(),
        pageLevel: "1"
      })
    }
    this.uniteLog(log)

    this.getCollectionList();
    this.getuserInfo()
  },
  //广告跳转
  gotoLink(){
    let type = this.data.configData && this.data.configData.extrecs[0].eleType || '';
    let url = this.data.configData && this.data.configData.extrecs[0].eleValue || '';
    if (type == 'link' && (url.substr(0, 5) == 'https' || url.substr(0, 4) == 'http')){
      if (url == 'http://wechat.daoran.tv/API_UBP/wx/mp/orderPage') {
        wx.navigateTo({
          url: '/pages/webView/webView'
        })
      } else {
        wx.navigateTo({
          url: '/pages/webView/webView?url=' + encodeURIComponent(url)
        })
      }
    }else{
      wx.navigateTo({
        url: url
      })
    }
  },
  //获取用户信息回调方法
  getUserInfoFun(e){
    if (e.detail.userInfo){
      user.getUser().then(res => {
        this.getuserInfo()
      })
      if (!e.currentTarget.dataset.goto) {
        wx.navigateTo({
          url: '/pages/setting/setting'
        })
      }
    }
  },
  // 获取用户数据
  getuserInfo() {
    
    if (wx.getStorageSync('userInfo')) {
      this.setData({
        userInfoShow: false,
        userInfo: JSON.parse(wx.getStorageSync('userInfo')),
        openid: JSON.parse(wx.getStorageSync('openid')),
      })
      let VIP = !wx.getStorageSync('VIP') ? {} : JSON.parse(wx.getStorageSync('VIP'));
      if (VIP.statusMap.lyhad && VIP.statusMap.lyhad.order != 0) {
        var time = Time.calcTime(VIP.statusMap.lyhad.validDate) * 1
        this.setData({
          timeshow:true
        })
        if (time < 4) {
          this.setData({
            surplusmunber: Math.floor(time),
            surplus: true
          })
          if (time < 1) {
            this.setData({
              surplusmunber: Math.floor(time * 24),
              mathHours: true
            })
          }
        }
      }
      console.log(this.data.VIP)
      this.setData({
        VIP: VIP
      })
    } else {
      this.setData({
        userInfoShow: true
      })
    }
  },
  gotoVip() {
    wx.navigateTo({
      url: '../webView/webView',
    })
  },
  //跳转到专辑详情页
  gotoAlbumDetails(e){
    console.log(e);
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/album_details/album_details?code='+item.code
    })
  },
  // 获取广告位
  getConfigPage() {
    let data = {
      streamNo: '',
      province: '',
      area: '',
      px: '',
      code: 'xcx_lhy1.0_jx'
    }
    http.lhxRequest("api_get", data).then(res => {
      if (res.code === 10000000) {
        console.log(res)
        this.setData({
          configData: res.page
        });
        console.log(this.data.configData.extrecs[0].imageVA);
      }
    })
  },
  //我的收藏
  getCollectionList: function () {
    let userData = wx.getStorageSync('openid') || '{}';
    userData = JSON.parse(userData);
    let data = {
      userId: app.globalData.memberId || userData.openId,
      resType: 5,
      mediaType: 1,
      project: "lyhad",
      px: 2,
      cur: 1,
      pageSize: 5
    }
    http.lhxRequest("api_getUserStoreResList", data).then(res => {
      if (res.code === 10000000) {
        let listInfo = res.albpb || {};
        listInfo.dataList = listInfo.dataList || [];
        listInfo.dataList.slice(0, 5);
        this.setData({
          collectionList: listInfo
        })
      }
    })
  },
  // 观看历史
  getHistoryList:function(){
    let userData = wx.getStorageSync('openid') || '{}';
    userData = JSON.parse(userData);
    let data = {
      userId: app.globalData.memberId || userData.openId,
      resType:5,
      mediaType:1,
      project:"lyhad",
      px:2,
      cur: 1,
      pageSize: 5
    }
    http.lhxRequest("api_getUserPlayHisResList", data).then(res => {
      if (res.code === 10000000) {
        let listInfo = res.albpb || {};
        listInfo.dataList = listInfo.dataList || [];
        listInfo.dataList.slice(0, 5);
        this.setData({
          historyList: listInfo
        })
      }
    })
  },
  // 跳转到历史记录
  gotoHistory: function(){
    wx.navigateTo({
      url: '/pages/history/history'
    })
  }, 
  // 跳转到我的收藏
  gotoCollection: function () {
    wx.navigateTo({
      url: '/pages/collection/collection'
    })
  },
  // 跳转到设置页面
  gotoSetting: function () {
    wx.navigateTo({
      url: '/pages/setting/setting'
    })
  },
  // 日志
  uniteLog(data) {
    http.lhxRequest("api_log", data).then(res => {
      if (res.code === 10000000) {
        console.log("日志")
      }
    })
  }
})